package com.zr.storehouse.controller;

import com.zr.storehouse.model.*;
import com.zr.storehouse.service.StoreService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/11.
 */
@RestController
public class StoreController {
    @Autowired
    private StoreService storeService;
/*
* 状态下拉列表
* */
    @PostMapping("iwm/getPullDownList")
    @CrossOrigin
    public ResultVO<List<StoreStatus>> getPullDownList(){
        List<StoreStatus> list=new ArrayList<>();
        StoreStatus storeStatus=new StoreStatus();
        storeStatus.setLabel("启用");
        storeStatus.setValue(true);
        StoreStatus ss=new StoreStatus();
        ss.setLabel("禁用");
        ss.setValue(false);
        list.add(storeStatus);
        list.add(ss);
        return ResultVOBuilder.success(list);
    }
    //查询库区属性
    @GetMapping("storageSection/queryBusinessAttribute")
    @CrossOrigin
    public ResultVO<List<ResAttribute>> queryBusinessAttribute(){
        return storeService.queryBusinessAttribute();
    }
    //明细分页
@PostMapping("storageSection/queryPage")
    @CrossOrigin
    public ResultVO<AllRecords> queryPage(@RequestBody  ResDetailsVo resDetailsVo){
return storeService.queryPage(resDetailsVo);
}
//修改
@PostMapping("storageSection/update")
    @CrossOrigin
    public ResultVO<ResDetailsUpdateVo> Update(@RequestBody ResDetailsUpdateVo resDetailsUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("200",bindingResult.getFieldError().getDefaultMessage());
        }
        return storeService.Update(resDetailsUpdateVo);

}
     //新增时库房下拉列表
    @GetMapping("storageSection/queryWareHouse")
        @CrossOrigin
    public ResultVO<List<StorageDetailsVo>> queryWareHouse(){
        return storeService.queryWareHouse();
    }
    //新增
    @PostMapping("storageSection/add")
    @CrossOrigin
    public ResultVO<ResDetailsUpdateVo> AddSection(@RequestBody @Valid ResDetailsUpdateVo resDetailsVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("298",bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println("TEST><><><><><><><"+resDetailsVo);
        return storeService.AddSection(resDetailsVo);
    }
    //查看和回显
    @GetMapping("storageSection/queryStorageSectionById")
    @CrossOrigin
    public ResultVO<ResDetailsVo> SeeSection(Integer id){
        return storeService.SeeSection(id);
    }
    //修改状态
    @PostMapping("storageSection/updateStatus")
    @CrossOrigin
    public ResultVO<ResDetailsVo> UpdateStatus(@RequestBody ResDetailsVo resDetailsVo){
        ResDetailsVo resDetailsVo1=storeService.findById(resDetailsVo.getId());
        storeService.updateStatus(resDetailsVo);
        return ResultVOBuilder.success(resDetailsVo1);
    }
}
